var PATH_PREFIX ="";

function setPathPrefix(path){
	PATH_PREFIX =  path;
}
$(function() {
	// 给select2组件加载样式
	WASP_WIDGET.initializeSelectStyle();
	// 注册清空事件
    WASP_WIDGET.registerResetClearEvent();
    
    //初始化日期控件
    //WASP_WIDGET.triggerDateStyleWithYMD("#q-createTime");
	WASP_WIDGET.triggerDateRangeStyle("q-createTime");
	
	$('#div_status').hide();
});

var $prdList = $('#prdList');
$(function() {
	$prdList.jqGrid({   
		url:PATH_PREFIX+'prdDemandListPage.do',
		//caption:'招商财富产品列表<button type="button" class="btn btn-default btn-sm btn-small btn-fullscreen" title="" tabindex="-1"><i class="fa fa-arrows-alt icon-fullscreen"></i></button>',  
		datatype: "json",
		colNames:["需求id","需求标题","需求类型","状态","创建日期","操作"],
		colModel:[  { name: 'demandId', index: 'demandId', hidden: true, key: true },
			        { name: 'title', index: 'title', width: 30, resizable: true, sortable: false },       
		            { name: 'typeNM', index: 'type', width: 30, resizable: true, sortable: false },
		            { name: 'statusNm', index: 'status', width: 30, resizable: true, sortable: false },
		            { name: 'createTime', index: 'createTime', width: 30, resizable: true, sortable: false },
		            { name: 'option', index: 'option', width: 30, resizable: true, sortable: false }
		            ],
		rowNum: 20,
        rowList: [20, 30, 50],
        rownumbers: true,
        rownumWidth: 50,
        prmNames: { search: "search", page: "pageNo", rows: "limit" },
        height: 'auto',
        width: false,
        autowidth:true,
        editurl: '',
        viewrecords: true,
        cellEdit: false,
        shrinkToFit: true,
        grouping: false,
        jsonReader: {
            root: "items", //结果集
            records: "total", //总记录数 
            total: "pageCount", //总页数
            page: "pageNo", //当前页 
            repeatitems: false // (4) 
        },  
		multiselect:false,
		pager: "#prdListPage",					
		viewrecords: true,
		hidegrid: false, 
		gridComplete: function(){
			var ids = $prdList.jqGrid('getDataIDs');
			for (var i = 0; i < ids.length; i++) {
                var id = ids[i];
                var rowData = $prdList.jqGrid('getRowData', id);
                var demandId = rowData.demandId;
                console.log("demandId："+demandId);
	            //var  detailBtn = '<a href="javascript:void(0);" class="btn btn-link btn-jqgrid" title="详情" onclick="WASP_DEMAND.detailView(\''+demandId+'\');">详情</a>';
//	            var  editBtn = '<a href="javascript:void(0);" class="btn btn-link btn-jqgrid" title="编辑" onclick="WASP_DEMAND.updateView(\''+demandId+'\');">编辑</a>';
//	            var  delBtn = '<a href="javascript:void(0);" class="btn btn-link btn-jqgrid" title="删除" onclick="WASP_DEMAND.deleteDemand(\''+demandId+'\');">删除</a>';
	            //var  downBtn = '<a href="javascript:void(0);" class="btn btn-link btn-jqgrid" title="下载" onclick="WASP_DEMAND.download(\''+demandId+'\');">下载</a>';

	            var params = {
					'templateDetail' : 'templateDetailTemp',
					'templateUpdate' : 'templateUpdateTemp',
					'templateDownload' : 'templateDownloadTemp',
					'templateDelete': 'templateDeleteTemp'
				};
				var permissionButton = Permission_Button.configPermissionButton(params);
				permissionButton.templateDetail = Permission_Button.clickPermissionButton('templateDetailTemp','WASP_DEMAND.detailView(\''+demandId+'\');');
				permissionButton.templateUpdate = Permission_Button.clickPermissionButton('templateUpdateTemp','WASP_DEMAND.updateView(\''+demandId+'\');');
				permissionButton.templateDownload = Permission_Button.clickPermissionButton('templateDownloadTemp','WASP_DEMAND.download(\''+demandId+'\');');
				permissionButton.templateDelete = Permission_Button.clickPermissionButton('templateDeleteTemp','WASP_DEMAND.deleteDemand(\''+demandId+'\');');
	            
	            $prdList.jqGrid('setRowData', ids[i], { option:Permission_Button.linkAllBtn(permissionButton)});
			}	
		}
	});
	$prdList.navGrid('#prdListPage',{edit:false,add:false,del:false,search:false,refreshstate:'current'});
	$prdList.jqGrid('setFrozenColumns');
	jqGridResize($prdList);
});

//绑定回车事件,查询
$("body").keypress(function(event){
	var key = event.which;
	if(key == "13"){
		queryByCondtion(true);
	}
});


/*
 * 条件查询
 */
function queryByCondtion(){
	var title = $.trim($("#q-title").val());
	var type = $("#q-type").val();
	var status = $("#q-status").val();
	var beginTime  = $.trim($("#q-createTime").getDateRangeValue()[0]); 
	var endTime  = $.trim($("#q-createTime").getDateRangeValue()[1]);
	debugger;
	
	var postData = $prdList.jqGrid("getGridParam", "postData");
	$.extend(postData,{
    	'sp[title]':title,
    	'sp[type]':type,
    	'sp[status]':status,
    	'sp[beginTime]':beginTime,
    	'sp[endTime]':endTime
    });
	$prdList.trigger("reloadGrid",[{page:1}]);  //重新载入Grid表格
}



var WASP_DEMAND = {
    addView: function() {
    	//var actionUrl = PATH_PREFIX + "demandHome.do?url=addDemand.do&oper=新增";
    	var actionUrl = PATH_PREFIX + "addDemand.do";
        window.open(actionUrl);
    },
    
    updateView: function(demandId) {
    	window.open(PATH_PREFIX + "updateView.do?demandId=" + demandId);  
//    	window.open(PATH_PREFIX + "demandHome.do?oper=编辑&url=updateView.do?demandId=" + demandId);  
    },
    download: function(demandId) {
    	window.open(PATH_PREFIX + "download.do?demandId=" + demandId);
    },
    deleteDemand: function(demandId) {
    	/** 删除 */
		ctools.confirm({title : "确认删除？", text: ""},
		function(isConfirm){
			if(isConfirm){
				$.ajax({
					type : "POST",
					dataType : "json",
					url : PATH_PREFIX + "delete.do",
					cache: false,
					data: {
						'demandId': demandId
					},
					success: function(data) {
						if (data.success) {
							swal(data.message, "", "success");
							queryByCondtion(false);
						} else {
							swal(data.message, "", "warning");
						}
					},
					error:function(xhr){
					   sweetAlertAjaxError(xhr);
					}
				});
			}
		});
    },

    detailView: function(demandId) {
    	window.open(PATH_PREFIX +"demandDetail.do?demandId=" + demandId);  
        //window.open(PATH_PREFIX +"demandHome.do?oper=产品创设需求详情&url=demandDetail.do?demandId=" + demandId);  
    }/*,

    batchDownload: function(ids) {
    	var ids = $prdList.jqGrid('getGridParam','selarrrow');
    	if (ids.length == 0) {
    		toastr.warning('', '请先勾选需下载的数据');
    		return;
    	}
    	
    	//window.open(PATH_PREFIX + "download.do?ids=" + ids);
    	
    	post(PATH_PREFIX + "download.do", {
    		'id': ids
    	});
    },*/
    
};


